var windowName = "task";
var Page ,taskList,idCheckLocal
apiready = function(){
    onWindowHide(function () {
    })
    console.log("index page is OK");
    taskList = new List();
    Page = createVM("#Page",{
        //页面数据

        banner:[
            {pic:picBanner,action:""}
        ],
        curTab:0,
        distanceOpen:false,
        timeOpen:false,
        sortOpen:false,
        list:[]

        //查询条件
        ,distance:0
        ,expire:0
        ,orderby:0
        ,keywords:""
        ,announce:[]
    },{
        //页面方法
        "init":function () {
            var banner = getLocData("bannerTask")
            if (isTrue(banner)) {
                this.banner = banner
            }

            var data =getData4Page();
            console.log(data.text);
            var lat = getLocData("lat")
            var lon = getLocData("lon")
            taskList.setUrl("/api/task/get/all").setData({
                lat:lat,
                lon:lon,
                distance:Page.distance,
                expire:Page.expire,
                orderby:Page.orderby,
                keywords:Page.keywords,
            }).setCbFunc(function (res) {
                stopPullDownRefresh();
                clearLoading();
                stopPullUpLoadMore();
                logJson(res,"list_"+ taskList.page)
                Page.list = res
                // getDistance()
            })

        }
        ,tab:function (index) {
            log(index)
            Page.curTab = index
            Page.subjectId = index
            startPullUpLoadMore();
            taskList.setData({}).reload();
        }
        ,onPullUpLoadMore:function () {
            startPullUpLoadMore();
            log('reach bottom')
            taskList.getList()
        }
        ,setDistance:function (num) {
            var lat = getLocData("lat")
            var lon = getLocData("lon")
            logJson(lat)
            logJson(lon)
            logJson(Page.distance)
            Page.distance = num;
            taskList.setData({
                lat:lat,
                lon:lon,
                distance:Page.distance,
                expire:Page.expire,
                orderby:Page.orderby,
                keywords:Page.keywords,
            }).reload();
            Page.distanceOpen = false;
            showLoading();
        }
        ,setExpire:function (num) {
            var lat = getLocData("lat")
            var lon = getLocData("lon")
            Page.expire = num;
            taskList.setData({
                lat:lat,
                lon:lon,
                distance:Page.distance,
                expire:Page.expire,
                orderby:Page.orderby,
                keywords:Page.keywords,
            }).reload();
            Page.timeOpen = false;
            showLoading();
        }
        ,setOrderby:function (orderby) {
            var lat = getLocData("lat")
            var lon = getLocData("lon")
            Page.orderby = orderby;
            taskList.setData({
                lat:lat,
                lon:lon,
                distance:Page.distance,
                expire:Page.expire,
                orderby:Page.orderby,
                keywords:Page.keywords,
            }).reload();
            Page.sortOpen = false;
            showLoading();
        }
        ,search:function () {
            var lat = getLocData("lat")
            var lon = getLocData("lon")
            taskList.setData({
                lat:lat,
                lon:lon,
                distance:Page.distance,
                expire:Page.expire,
                orderby:Page.orderby,
                keywords:Page.keywords,
            }).reload();
            Page.sortOpen = false;
            showLoading();
        }

    })

    Page.init()
    onPullUpLoadMore(function () {
        Page.onPullUpLoadMore()
    })
    onPullDownRefresh(function () {

        var lat = getLocData("lat")
        var lon = getLocData("lon")
        taskList.setData({
            lat:lat,
            lon:lon,
            distance:Page.distance,
            expire:Page.expire,
            orderby:Page.orderby,
            keywords:Page.keywords,
        }).reload();
    })
}
function updateWinData() {
    var banner = getLocData("bannerTask")
    if (isTrue(banner)) {
        Page.banner = banner
    }
    var announce = getLocData("announce");if (isTrue(announce))Page.announce = announce;
    apiGet("/app/announce",{event:[1]},function (res,code) {
        logJson(res,"announce_")
        if (code == 200) {
            Page.announce = res.data;
            setLocData("announce",Page.announce);
        }
    })
    showLoading();
    taskList.reload();
}

function getDistance() {
    // var lat = getLocData("lat")
    // var lon = getLocData("lon")
    // if (!isTrue(lat)) {
    //     showToast("未获取到位置信息，请检查是否授权");
    //     return;
    // }
    // Page.list.map(function (data,index) {
    //     data.km = GetDistances(lat,lon,data.address.lat,data.address.lon)
    // })
}
